/**
 * Created by liuzhentao on 2016/7/7.
 */

// getting-started.js
var mongoose = require('mongoose');
//mongodb://user:pass@localhost:port/database
mongoose.connect('mongodb://test:test@localhost/test');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
    console.info('connected......');
});

var kittySchema = mongoose.Schema({
    name: String
});


kittySchema.methods.speak = function () {
    var greeting = this.name
        ? "Meow name is " + this.name
        : "I don't have a name";
    console.log(greeting);
}

var Kitten = mongoose.model('Kitten', kittySchema);

//2 var silence = new Kitten({ name: 'Silence' });

var silence = new Kitten();


/*silence.save(function (err,silence) {
    if (err) return console.error(err);
    silence.speak();
    console.log("silence.name--->"+silence.name)
})*/

//Model.find(query, fields, options, callback)
// fields 和 options 都是可选参数
silence.find({name:'Silence'},function (err,docs) {
    console.log("docs--->"+JSON.stringify(docs))
})
